package cn.piesat.scanning.business.dq1043;

import cn.piesat.scanning.business.common.AlgorithmDefineAbstract;
import cn.piesat.scanning.business.common.AlgorithmExecAbstract;
import cn.piesat.scanning.business.common.algorithm.cimissScan.CimissScan;
import cn.piesat.scanning.business.common.algorithm.dataClear.DataClear;
import cn.piesat.scanning.business.common.algorithm.dataDistribute.FtpPublish;
import cn.piesat.scanning.business.common.algorithm.dataPartition.DataPartition;
import cn.piesat.scanning.business.common.algorithm.dataTransfer.DataTransfer;
import cn.piesat.scanning.business.common.algorithm.dataUnzip.DataUnzip;
import cn.piesat.scanning.business.dq1043.algorithm.DataScanAlgorithm;
import cn.piesat.scanning.business.dq1043.algorithm.FtpScanAlgorithm;
import cn.piesat.scanning.business.dq1043.flow.FlowTask;
import cn.piesat.scanning.business.dq1043.single.SingleTask;
import cn.piesat.scanning.dto.DbDmsSchedulerTaskFileRecordDTO;
import cn.piesat.scanning.dto.HtAlgorithmConfigurationDTO;
import cn.piesat.scanning.dto.HtScheduleJobDTO;
import cn.piesat.scanning.service.DbDmsSchedulerTaskFileRecordService;
import cn.piesat.scanning.service.HtAlgorithmConfigurationService;
import cn.piesat.scanning.service.HtScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 1043项目算法执行选择
 *
 * @author xhl
 */
@Component
public class AlgorithmExec extends AlgorithmExecAbstract {

    @Autowired
    private DataUnzip dataUnzip;
    @Autowired
    private DataPartition dataPartition;
    @Autowired
    private DataTransfer dataTransfer;
    @Autowired
    private DataClear dataClear;
    @Autowired
    private DataScanAlgorithm dataScanAlgorithm;
    @Autowired
    private FtpScanAlgorithm ftpScanAlgorithm;
    @Autowired
    private FtpPublish ftpPublish;
    @Autowired
    private CimissScan cimissScan;
    @Autowired
    private HtScheduleJobService htScheduleJobService;
    @Autowired
    private FlowTask flowTask;
    @Autowired
    private SingleTask singleTask;
    @Autowired
    private HtAlgorithmConfigurationService htAlgorithmConfigurationService;
    @Autowired
    private DbDmsSchedulerTaskFileRecordService dbDmsSchedulerTaskFileRecordService;

    private static Map<String, AlgorithmDefineAbstract> methodsMap = new HashMap<>();

    @PostConstruct
    public void init() {
        methodsMap.put("datascan",dataScanAlgorithm);
        methodsMap.put("ftpscan",ftpScanAlgorithm);
        methodsMap.put("ftpPublish",ftpPublish);
        methodsMap.put("clearData",dataClear);
        methodsMap.put("dataPartition",dataPartition);
        methodsMap.put("dataTransfer",dataTransfer);
        methodsMap.put("dataunzip",dataUnzip);
        methodsMap.put("cimissscan",cimissScan);
    }

    @Override
    public void selectAlgorithm(String taskId) {
        HtScheduleJobDTO taskDTO = htScheduleJobService.findById(taskId);
        if(taskDTO!=null){
            String taskType = taskDTO.getTaskType();
            if ("flowTask".equals(taskType) || "redo_flowTask".equals(taskType)) {
                flowTask.process(taskId);
            } else if ("singleTask".equals(taskType) || "redo_singleTask".equals(taskType)){
                HtAlgorithmConfigurationDTO algDTO = htAlgorithmConfigurationService.findById(taskDTO.getAlgoId());
                String algorithmMark = algDTO.getAlgorithmIdentify();
                if (methodsMap.containsKey(algorithmMark)) {
                    Map<String, Object> inputParams = getInputParams(taskId);
                    methodsMap.get(algorithmMark).exec(inputParams,taskId);
                } else {
                    singleTask.process(taskId);
                }
            }
        }
    }

    @Override
    public void redo(String recordId) {
        DbDmsSchedulerTaskFileRecordDTO record = dbDmsSchedulerTaskFileRecordService.findById(recordId);
        if (record == null) {
            return ;
        }
        HtScheduleJobDTO taskDTO = htScheduleJobService.findById(record.getTaskId());
        String taskType = taskDTO.getTaskType();
        if ("flowTask".equals(taskType) || "redo_flowTask".equals(taskType)) {
            flowTask.redo(recordId);
        } else if ("singleTask".equals(taskType) || "redo_singleTask".equals(taskType)) {
            singleTask.redo(recordId);
        }
    }

}
